package JavaGuide.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, NoSuchFieldException {
//        Class<User> clazz= User.class;//不会触发类的初始化
//        Class<?> clazz1=Class.forName("JavaGuide.reflect.User");
//        Field[] declaredFields = clazz1.getDeclaredFields();
//        Object object=clazz1.getConstructor().newInstance();
//        User user=(User) object;
//        Method method=clazz1.getMethod("publicMethod");
//        Method staticMethod=clazz1.getMethod("publicStaticMethod");
//        method.invoke(user);
//        staticMethod.invoke(null);

//        User user=new User();
//        Class<?> clazz2=user.getClass();
//        Address address=new Address("9090","0909");
//        Customer customer=new Customer("haha","9090@com");
//        Order order=new Order(address,customer);
//        order.getAddress().printStreet();
//        order.getCustomer().printName();

//
        Container container=new Container();
        container.init();
//        Class<?> clazz=Class.forName("JavaGuide.reflect.Customer");
//        Object obj=container.getServiceInstanceByClass(clazz);
        Class<?> clazz=Class.forName("JavaGuide.reflect.Order");
        Object obj=container.createInstance(clazz);
        Field field=clazz.getDeclaredField("customer");
        System.out.println("---------");
        System.out.println(field);
        System.out.println("---------");
        field.setAccessible(true);
        Object fieldValue=field.get(obj);
        System.out.println(fieldValue);
        Method[] methods=fieldValue.getClass().getDeclaredMethods();
        for (Method method:methods){
            if(method.getDeclaredAnnotation(PrintAble.class)!=null){
                System.out.println(method.getName());
                method.invoke(fieldValue);
            }

        }



    }
}
